package com.ts.hidden.fragment.login;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ts.hidden.MsConst;
import com.ts.hidden.R;
import com.ts.tube.utils.PreferenceUtil;

public class RestoreFragment extends Fragment implements OnClickListener {

    private ImageView imgNext;
    private String email;
    private TextView txtEmail;
    private boolean isRestore = false;

    public static RestoreFragment newInstance(boolean isRestore) {
        RestoreFragment f = new RestoreFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("is", isRestore);
        f.setArguments(bundle);
        return f;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null) {
            email = savedInstanceState.getString("email");
        }
        
        isRestore = getArguments().getBoolean("is");
        
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if(!TextUtils.isEmpty(txtEmail.getText())) {
            outState.putString("email", txtEmail.getText().toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.restore_fragment, container, false);
        initLayout(rootView);
        initListener();
        return rootView;
    }

    private void initLayout(View rootView) {
        imgNext = (ImageView) rootView.findViewById(R.id.login_page_restore_next);
        AnimationDrawable animationDrawable = (AnimationDrawable) imgNext.getBackground();
        animationDrawable.start();
        
        txtEmail = (TextView) rootView.findViewWithTag("email");
        txtEmail.setText(email);
    }

    private void initListener() {
        imgNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.login_page_restore_next:
            if(isEmailValid(txtEmail.getText())) {
                if(isRestore) {
                    String oldEmail = PreferenceUtil.getPreference(getActivity(), MsConst.KEY_EMAIL, "default");
                    if (oldEmail.equalsIgnoreCase(txtEmail.getText().toString())) {
                        listener.onIRestore();
                    } else {
                        Toast.makeText(getActivity(), "Your Email is INCORRECT !!!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    
                    PreferenceUtil.setPreference(getActivity(), MsConst.KEY_EMAIL, txtEmail.getText().toString());
                    listener.onIRestoreNext();
                }
            } else {
                Toast.makeText(getActivity(), "Your Email is not valid", Toast.LENGTH_SHORT).show();
            }
            break;

        default:
            break;
        }
    }

    public interface IRestoreListener {
        void onIRestoreNext();
        void onIRestore();
    }
    private IRestoreListener listener;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof IRestoreListener) {
            listener = (IRestoreListener) activity;
        }
    }
    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }
    boolean isEmailValid(CharSequence email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
     }
}
